//
//  RegisterViewController.swift
//  ShiZhe
//
//  Created by 陈卿伟 on 16/8/29.
//  Copyright © 2016年 陈卿伟. All rights reserved.
//

import UIKit



class RegisterViewController: MainViewController {
    var registerView:RegisterView!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationController?.navigationBarHidden = false
        self.title = "注册"
        self.addRegisterView()
        self.addClickEventForRegisterView()
    }
}

//MARK:--UI
extension RegisterViewController{
    //添加UI进来
    func addRegisterView(){
        registerView = RegisterView(frame: MainBounds)
        self.view.addSubview(registerView)
    }
    
    //为register的各个控件添加点击事件
    func addClickEventForRegisterView(){
        //下一步按钮添加点击事件
        registerView.nextButton.addTarget(self, action: #selector(self.nextButtonClick(_:)), forControlEvents: .TouchUpInside)
        
        //注册协议添加点击事件
        let tapG = UITapGestureRecognizer(target: self, action: #selector(self.protocolLabelClick(_:)))
        registerView.addGestureRecognizer(tapG)
        
    }
}


//MARK:--UI
extension RegisterViewController{
    //下一步按钮点击事件
    func nextButtonClick(sender:UIButton){
        let phoneText = registerView.phoneNumber
        let password = registerView.password
        let password2 = registerView.passwordTwo
        if phoneText?.text!.characters.count != 11{
            showHUD.showTopHud(self.view,text: "请输入正确的手机号码")
        }else if password?.text != password2?.text{
            showHUD.showTopHud(self.view,text: "两次密码输入不一致")
        }else if password?.text!.characters.count<6{
            showHUD.showTopHud(self.view,text: "请输入大于6位数的密码")
        }else if password?.text!.characters.count>20{
            showHUD.showTopHud(self.view,text: "请输入小于20位的密码")
        }else{
            phoneText?.resignFirstResponder()
            password?.resignFirstResponder()
            password2?.resignFirstResponder()

            //发送验证码
            let hud = showHUD.showWaitHud(self.view)
            SMSSDK.getVerificationCodeByMethod(SMSGetCodeMethodSMS, phoneNumber: phoneText?.text, zone: "86", customIdentifier: nil) { (error) in
                hud.alpha = 0
                if error == nil{
                    let vc = VCodeViewController()
                    vc.phoneNum = phoneText?.text
                    vc.password = password.text
                    self.navigationController?.pushViewController(vc, animated: true)
                }else{
                    let userInfo = error.userInfo["getVerificationCode"] as! String
                    showHUD.showTopHud(self.view, text: userInfo)
                }
            }
        }
    }
    
    //注册协议点击事件
    func protocolLabelClick(sender:UITapGestureRecognizer){
        print("点击了注册协议")
    }
}






















